package com.spa.modules.system.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.spa.common.api.vo.Result;
import com.spa.common.aspect.annotation.AutoLog;
import com.spa.modules.base.entity.User;
import com.spa.modules.system.service.SpaUserService;

import cn.dev33.satoken.stp.StpUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

/**
 * 管理后台登录相关
 * 
 * @author P0081447
 *
 */
@RestController
@RequestMapping("/sys")
@Api(tags = "管理后台登录相关")
public class IndexController {

	@Autowired
	private SpaUserService spaUserService;

	/**
	 * 获取用户信息
	 * 
	 * @return
	 */
	@ApiOperation("获取用户信息")
	@GetMapping("/getUserInfo")
	@AutoLog(value = "获取用户信息")
	public Result<Object> userInfo() {
		return spaUserService.findUserInfo();
	}

	/**
	 * 用户登录
	 * 
	 * @return
	 */
	@ApiOperation("用户登录")
	@PostMapping("/login")
	@AutoLog(value = "用户登录")
	public Result<Object> login(@RequestBody User user) {
		return spaUserService.checkSysUser(user, "system");
	}
	
	/**
	 * 用户注销登录
	 * 
	 * @return
	 */
	@ApiOperation("管理员注销登录")
	@GetMapping("/logout")
	@AutoLog(value = "管理员注销登录")
	public Result<Object> logout() {
		Integer id = Integer.parseInt(StpUtil.getLoginId().toString());
		StpUtil.logout(id, "system");
		return Result.resultOK("ok");
	}

}
